package com.guigu.buy.utils;

import com.guigu.buy.service.IOrderService;
import com.guigu.buy.pojo.Product;
import com.guigu.buy.service.IOutGatherService;
import com.guigu.buy.service.IProductService;
import com.guigu.buy.service.IPurchaseOrderService;
import com.guigu.buy.service.IStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class FileAddUtil {
    @Autowired
    private IPurchaseOrderService purchaseOrderService;
    @Autowired
    private IStorageService storageService;
    @Autowired
    private IOutGatherService outGatherService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IOrderService iOrderService;


    /**
     * 生成各种设计单编号：
     * 采购订单设计单编号	100+当前日期+4位流水号
     * @param type 设计单类型编号
     * @return
     */
    public String getTypeNumber(String type) {
        System.out.println(type);
        // 拼接类型编号100
        StringBuffer buffer = new StringBuffer(type);

        // 当前日期 格式：YYYYMMDD
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(now);
        buffer.append(date);
        System.out.println(date);
        // 根据类型获取对应设计单的最大的设计单编号
        String designId = "";
        switch (type) {
            case "100":
                designId=purchaseOrderService.getMaxDesignId();
                break;
            case "300":
                designId=storageService.getMaxDesignId();
                break;
            case "301":
                designId=iOrderService.getMaxStockId();
                break;
            case "400":
                designId=outGatherService.getMaxDesignId();
                break;
            case "700":
                designId=productService.getMaxDesignId();
                break;
        }
        String number = "";
        System.out.println(designId);
        if(designId != null ){
            // 获取当前设计单编号的4位流水号  xxxYYYYMMDDxxxx
            String flowNumber = designId.substring(11);
            // 获取当前设计单编号的年份
            String year = designId.substring(3, 7);
            if(year.equals(date.substring(0,4))) {
                int temp = Integer.parseInt(flowNumber);
                temp ++;
                // 补齐4位字符串
                String newFlowNumber = String.format("%04d", temp);
                number = buffer.append(newFlowNumber).toString();
                System.out.println(number);
            }else{
                number = buffer.append("0001").toString();
            }
        }else {
            number = buffer.append("0001").toString();
        }
        return number;
    }
    /**
     * 生成前台订单编号
     * @return
     */
    public String createOrderId(){
        String newOrderId = "";
        String maxOrderId = iOrderService.getMaxStockId();
        if (maxOrderId == null) {
            newOrderId = "300";
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            newOrderId += dateFormat.format(date) + "001";
        } else {
            String str = maxOrderId.substring(3, 11);
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            String format = dateFormat.format(date);
            if(str.equals(format)){
                int temp = Integer.parseInt(maxOrderId.substring(11));
                temp++;
                newOrderId = "300" + str + String.format("%03d", temp);
            }else{
                newOrderId = "300" + format + "001";
            }
        }
        return newOrderId;
    }
}
